using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using XNAProject;
using XNAProjectLibrary;


namespace XNAProject.Core
{
    public class XNAProjectPointer : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Texture2D texture;
        Texture2D pointer;
        Texture2D pointerSide;
        Vector2 mousePosition;

        float rotation = 0f;

        XNAGame OurGame;
        IInputHandler Input;

        public XNAProjectPointer(Game game)
            : base(game)
        {
            mousePosition = Vector2.Zero;                                                  //set up mouse position   

            OurGame = (XNAGame)game;
            Input = (IInputHandler)game.Services.GetService(
                typeof(IInputHandler));
        }

        protected override void LoadContent()
        {
            pointer = Game.Content.Load<Texture2D>("Textures/pointer");                                 //load pointer texture
            pointerSide = Game.Content.Load<Texture2D>("Textures/pointerSide");

            texture = pointer;

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            mousePosition.X = Input.MouseState.X;                                                       //update pointer position
            mousePosition.Y = Input.MouseState.Y;

            if (mousePosition.X < 0)
            {
                mousePosition.X = pointerSide.Width;
                texture = pointerSide;
                rotation = 3.2f;
            }
            else if (mousePosition.X > Game.Window.ClientBounds.Width)
            {
                mousePosition.X = Game.Window.ClientBounds.Width - pointerSide.Width;
                texture = pointerSide;
                rotation = 0;
            }
            else if (mousePosition.Y < 0)
            {
                mousePosition.Y = pointerSide.Width;
                texture = pointerSide;
                rotation = 4.7f;
            }
            else if (mousePosition.Y > Game.Window.ClientBounds.Height)
            {
                mousePosition.Y = Game.Window.ClientBounds.Height - pointerSide.Width;
                texture = pointerSide;
                rotation = 7.9f;
            }
            else
            {
                rotation = 0;
                texture = pointer;
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            OurGame.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

            OurGame.spriteBatch.Draw(texture, mousePosition, null, Color.White, rotation, Vector2.Zero, 1f, SpriteEffects.None, 0f);

            OurGame.spriteBatch.End();            

            base.Draw(gameTime);
        }
    }
}